package com.woniu.controller.config.interceptor;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import com.woniu.mapper.mallSystem.CategoryMapper;
import com.woniu.model.mallSystem.entity.Category;
import com.woniu.model.mallSystem.gh.annotation.UpdateCommodityESAnnotation;
import com.woniu.model.mallSystem.gh.annotation.UpdateCommodityTypeCacheAnnotation;
import com.woniu.model.mallSystem.gh.common.RedisConstant;
import com.woniu.model.mallSystem.vo.CommodityVo;
import com.woniu.service.mallSystem.impl.CommodityServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

// 更新缓存拦截器
public class UpdateCacheInterceptor implements HandlerInterceptor {

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private CategoryMapper categoryMapper;

    @Autowired
    private CommodityServiceImpl commodityService;

    @Autowired
    private ElasticsearchRestTemplate esTemplate;



    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerMethod hmd = (HandlerMethod) handler;

        // 检查方法上是否有自定义的更新缓存注解，有则更新redis缓存
        if (hmd.getMethod().isAnnotationPresent(UpdateCommodityTypeCacheAnnotation.class)){
            redisTemplate.delete(RedisConstant.CATEGORY_ALL_PLIST);

            List<Category> list = categoryMapper.selectList(Wrappers.lambdaQuery(Category.class)
                    .eq(Category::getParentCategoryId, -1));
            // 存入redis
            redisTemplate.opsForList().leftPush(RedisConstant.CATEGORY_ALL_PLIST, list);
        }

        // 检查方法上是否有自定义的更新缓存注解, 有则更新es缓存
        if (hmd.getMethod().isAnnotationPresent(UpdateCommodityESAnnotation.class)){

            // 根据删除指定类型索引
            esTemplate.indexOps(CommodityVo.class).delete();

//            Runnable runnable = new Runnable() {
//                @Override
//                public void run() {
//                    try {
//                        Thread.sleep(3000);
//                    } catch (InterruptedException e) {
//                        throw new RuntimeException(e);
//                    }
//                    GoodsDTO goodsDTO = new GoodsDTO();
//                    goodsDTO.setPageNum(1);
//                    goodsDTO.setPageSize(Integer.MAX_VALUE);
//                    Page pageInfo = goodsService.getPageInfo(goodsDTO);
//                    List<GoodsVO> records = pageInfo.getRecords();
//
//                    esTemplate.save(records);
//                }
//            };
//            Thread thread = new Thread(runnable);
//            thread.start();


//            CommodityVo goodsDTO = new CommodityVo();
//            goodsDTO.setPageNum(1);
//            goodsDTO.setPageSize(Integer.MAX_VALUE);
//            Page pageInfo = commodityService.findAllCommodities();
//            List<GoodsVO> records = pageInfo.getRecords();

//            esTemplate.save(records);

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }


    }
}
